if orders and not orders.GetTakeLimit then


function orders:GetTakeLimitID(order_num)
	if order_num then
		local num = order_num
		
		if type(order_num) == "table" then
			num = order_num.order_num
		end
		
		local limits = self:LoadTakeLimits()
		return limits["s" .. tostring(num)]
	end
end


function orders:GetTakeLimitStopID(order_num)
	if order_num then
		local num = order_num
		
		if type(order_num) == "table" then
			num = order_num.order_num
		end
		
		local limits = self:LoadTakeLimits()
		return limits["l" .. tostring(num)]
	end
end


function orders:GetTakeLimit(order)
	if order and order.order_num then
		local id = self:GetTakeLimitID(order.order_num)
		local order = self:GetLimit(id)
		
		if id and not order then
			self:CleanTakeLimits()
		end
		
		return order
			--[[
		local limit = self:GetTakeLimitID(order.order_num)
		if limit then
			local filter_str = [ [
				local order = ...
				return order.order_num == %s
			] ]
			local filter = load(
				string.format(filter_str, tostring(limit))
			)
			limits = lib:GetTable("orders", filter)
			if limits and #limits > 0 then
				return limits[1]
			end
		end
			--]]
	end
end


function orders:GetTakeLimitStop(order)
	if order and order.order_num then
		local id = self:GetTakeLimitStopID(order.order_num)
		local order = self:GetStop(id)
		
		if id and not order then
			self:CleanTakeLimits()
		end
		
		return order
		--[[
		local limit = self:GetTakeLimitStopID(order.order_num)
		if limit then
			local filter_str = [ [
				local order = ...
				return order.order_num == %s
			] ]
			local filter = load(
				string.format(filter_str, tostring(limit))
			)
			limits = lib:GetTable("stop_orders", filter)
			if limits and #limits > 0 then
				return limits[1]
			end
		end
		--]]
	end
end


function orders:LoadTakeLimits(nounlock)
	if not self.take_limit_file then
		self.take_limit_sem = "take_limit." .. lib:GetUser()
		self.take_limit_file = lib.path.init .. self.take_limit_sem .. ".dat"
	end
	
	lib:lock(self.take_limit_sem)
		
	local ret = lib:LoadIni(self.take_limit_file)
	
	if not nounlock then
		lib:unlock(self.take_limit_sem)
	end
	
	return ret
end


function orders:SaveTakeLimit(stop_num, limit_num)
	local limits = self:LoadTakeLimits(true)
	local num
			
	if stop_num then
		num = limits["s" .. tostring(stop_num)]
		limits["s" .. tostring(stop_num)] = nil
		limits["l" .. tostring(num)] = nil
	end
	if limit_num then
		num = limits["l" .. tostring(limit_num)]
		limits["s" .. tostring(num)] = nil
		limits["l" .. tostring(limit_num)] = nil
	end
	if stop_num and limit_num then
		limits["s" .. tostring(stop_num)] = limit_num
		limits["l" .. tostring(limit_num)] = stop_num
	end
	
	lib:SaveFile(self.take_limit_file, lib:tostring(limits))
	
	lib:unlock(self.take_limit_sem)
end


function orders:CleanTakeLimits()
	local limits = self:LoadTakeLimits()
	
	local orders = lib:GetTable("orders", "order_num")
	local stops = lib:GetTable("stop_orders", "order_num")
	
	local num
	
	--self.log("CleanTakeLimit", limits, orders, stops)
	
	for k, v in pairs(limits) do
		num = int(string.match(k, "^s(%d+)"))
		if num > 0 and (not orders[v] or bit.band(orders[v].flags, 3) ~= 1) then
			self:SaveTakeLimit(nil, v)
			orders:KillStopOrder(stops[num])
		end
		num = int(string.match(k, "^l(%d+)"))
		if num > 0 and (not stops[v] or bit.band(stops[v].flags, 3) ~= 1) then
			self:SaveTakeLimit(v)
			orders:KillOrder(orders[num])
		end
	end
	
	--lib:SaveFile(self.take_limit_file, lib:tostring(limits))
end


end -- of file
